package com.linln.modules.activityticket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linln.modules.system.domain.User;
import com.linln.modules.system.repository.UserRepository;
import com.linln.modules.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.modules.activityticket.repository.ActivityTicketMapper;
import com.linln.modules.activityticket.domain.ActivityTicket;
import com.linln.modules.activityticket.service.ActivityTicketService;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.activityticket.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.activityticket.service.impl
 * @Description: TODO
 * @date Date : 2020年12月23日 15:51
 */

@Service
public class ActivityTicketServiceImpl extends ServiceImpl<ActivityTicketMapper, ActivityTicket> implements ActivityTicketService {

    @Autowired
    private ActivityTicketMapper activityTicketMapper;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;




    @Override
    public boolean addActivityTicketService(ActivityTicket activityTicket) {
        return this.save(activityTicket);
    }

    @Override
    @Transactional
    public void batchAddActivityTicketService(List<ActivityTicket> activityTicketList,List<User> userList) {
        userService.save(userList);
        this.saveBatch(activityTicketList);
    }

    @Override
    public boolean delActivityTicketService(String activityTicketId) {
        return this.removeById(activityTicketId);
    }

    @Override
    public boolean updateActivityTicketService(ActivityTicket activityTicket) {
        return this.updateById(activityTicket);
    }

    @Override
    public Map<String, Object> getActivityTicket(Integer pageNum, Integer limit, String activityTicketName) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(pageNum, limit);
        PageInfo<ActivityTicket> pageInfo = new PageInfo<>(activityTicketMapper.selectActivity(activityTicketName));
        map.put("data", pageInfo.getList());
        map.put("count", pageInfo.getTotal());
        map.put("code", 0);
        return map;
    }


    @Override
    public boolean useRuleIsItAssociatedByActicityTicket(String useRuleId) {
        List<ActivityTicket> at_use_rule_id = this.list(new QueryWrapper<ActivityTicket>().eq("at_use_rule_id", useRuleId));
        return at_use_rule_id != null && at_use_rule_id.size() > 0;
    }

    @Override
    public List<ActivityTicket> getCanGiveActivity() {
        return activityTicketMapper.selectCanGiveActivity();
    }

    @Override
    public int buyIntegralService(String at_id) {
        return activityTicketMapper.buyIntegralService(at_id);
    }
}
